package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.utill.MD5;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @Description: java类作用描述
 * @Author: wuyuanhao
 * @CreateDate: 2020/8/11$ 18:12$
 * @UpdateUser: wuyuanhao
 * @UpdateDate: 2020/8/11$ 18:12$
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int addUser(User user) {
        //md5加密
        user.setPassword(MD5.getMD5String(user.getPassword()));
        return userMapper.insert(user);
    }

    @Override
    public User login(User user) {
        //md5加密
        user.setPassword(MD5.getMD5String(user.getPassword()));
        return userMapper.login(user);
    }

    @Override
    public List<User> selectUserList(User user) {

        return userMapper.selectUserList(user);
    }

    @Override
    public User seUserById(int id) {
        return userMapper.seUserById(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteById(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public String saveFile(MultipartFile filedata, String pathval) {
        File fileDir = new File(pathval);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        // 上传的文件名
        String filename = String.valueOf(System.currentTimeMillis())+".jpg";
        // 总的文件路径
        String filePath = pathval + "/" + filename;
        File file = new File(filePath);
        try {
            FileOutputStream out = new FileOutputStream(file);
            // 写入文件
            out.write(filedata.getBytes());
            out.flush();
            out.close();
            return "/upload/"+filename;// 返回图片保存路径
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
